package io.renren.modules.hos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.utils.CodeUtils;
import io.renren.modules.hos.entity.DoctorEntity;
import io.renren.modules.hos.form.AppointmentForm;
import io.renren.modules.hos.service.DoctorService;
import io.renren.modules.hos.service.TitleAmountService;
import io.renren.modules.hos.vo.AppointmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.AppointmentDao;
import io.renren.modules.hos.entity.AppointmentEntity;
import io.renren.modules.hos.service.AppointmentService;


@Service("appointmentService")
public class AppointmentServiceImpl extends ServiceImpl<AppointmentDao, AppointmentEntity> implements AppointmentService {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private TitleAmountService amountService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AppointmentEntity> page = this.page(
                new Query<AppointmentEntity>().getPage(params),
                new QueryWrapper<AppointmentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByPatientId(Long id, Map<String, Object> params) {
        LambdaQueryWrapper<AppointmentEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppointmentEntity::getPatientId, id);
        List<AppointmentEntity> appointmentEntities = baseMapper.selectList(queryWrapper);
        int pageSize = (int) params.get("pageSize");
        int pageIndex = (int) params.get("pageIndex");
        List<AppointmentVo> appointmentVos = patientCopyList(appointmentEntities);
        return new PageUtils(appointmentVos, appointmentVos.size(), pageSize, pageIndex);
    }

    @Override
    public AppointmentEntity addAppointment(Long id, AppointmentForm form) {
        AppointmentEntity appointmentEntity = new AppointmentEntity();
        DoctorEntity doctorEntity = doctorService.getById(form.getDoctorId());

        appointmentEntity.setAmount(amountService.selectAmountByTitle(doctorEntity.getDoctorTitle()));
        appointmentEntity.setDoctorId(form.getDoctorId());
        appointmentEntity.setPatientId(id);
        appointmentEntity.setCreateTime(new Date());
        appointmentEntity.setFetchAddress("门诊楼一楼自助机");
        appointmentEntity.setReserveDate(form.getReserveDate());
        appointmentEntity.setReserveTime(form.getReserveTime());
        appointmentEntity.setOrderNum(CodeUtils.getOrderCode());
        this.save(appointmentEntity);
        return appointmentEntity;
    }

    @Override
    public AppointmentEntity selectByOrderNum(String out_trade_no) {
        LambdaQueryWrapper<AppointmentEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppointmentEntity::getOrderNum, out_trade_no);

        return baseMapper.selectOne(queryWrapper);
    }

    public AppointmentVo patientCopy(AppointmentEntity appointmentEntity){
        DoctorEntity doctorEntity = doctorService.getById(appointmentEntity.getDoctorId());
        AppointmentVo appointmentVo = new AppointmentVo();
        BeanUtils.copyProperties(appointmentEntity, appointmentVo);
        appointmentVo.setDoctorName(doctorEntity.getDoctorName());
        appointmentVo.setDoctorImg(doctorEntity.getDoctorImg());
        appointmentVo.setDep(doctorEntity.getDoctorDepartment());
        appointmentVo.setTitle(doctorEntity.getDoctorTitle());

        return appointmentVo;
    }

    public List<AppointmentVo> patientCopyList(List<AppointmentEntity> appointmentEntities){
        List<AppointmentVo> appointmentVos = new ArrayList<>();
        for (AppointmentEntity appointmentEntity : appointmentEntities) {
            appointmentVos.add(patientCopy(appointmentEntity));
        }
        return appointmentVos;
    }

}